<?php
$model=new Adviser();
$cs=Yii::app()->clientScript;
$cs->registerCoreScript('jquery');
$dir = Yii::getPathOfAlias('ext.ueditor');
$baseUrl = Yii::app()->getAssetManager()->publish($dir);
$cs->registerScriptFile($baseUrl.'/editor_all.js',CClientScript::POS_HEAD );
$cs->registerScriptFile($this->createUrl('site/ueditor', array('_'=>md5(rand(),rand()))),CClientScript::POS_HEAD );
$dir = Yii::getPathOfAlias('ext.ueditor.themes.default');
$baseUrl = Yii::app()->getAssetManager()->publish($dir);
$cs->registerCssFile($baseUrl.'/ueditor.css');
$cs->registerScript('ueditor', 'var ue = new UE.ui.Editor();ue.render(\'Adviser_info\');', CClientScript::POS_READY);

$pid=Yii::app()->request->getParam('pid', 0);
$cate=Yii::app()->request->getParam('cate', 0);
$iid=Yii::app()->request->getParam('iid', 0);
$id=Yii::app()->request->getParam('id', 0);
if(!empty($id)){
    $model = Adviser::model()->findByPk($id);
    if(isset($_POST['Adviser'])){
        $photo_old = $model->photo;
        $model->attributes=$_POST['Adviser'];

        $photo = CUploadedFile::getInstance($model, 'photo');
        if(!empty($photo)){
            if(!empty($photo_old)){
                @unlink(Yii::app()->getBasePath().'/..'.$photo_old);
            }
            $photo_root = Yii::app()->getBasePath().'/../images/user/zc/';
            if(!is_dir($photo_root))
                mkdir($photo_root,0777,true);
            $newName  = md5(rand(1,100).date('YmdHis').rand(1,1000)).'.'.$photo->extensionName;
            if (is_object($photo)) {
                $photo->saveAs($photo_root.$newName);
                $model->photo='/images/user/zc/'.$newName;
            }
        }

        if(empty($model->photo)){
            $model->photo=$photo_old;
        }
        if($model->save()){
            $this->oz_info('修改成功',$this->createUrl('manage/index', array('pid'=>$pid, 'cate'=>23, 'iid'=>2)));
            return;
        }
    }
}

?>

<style type="text/css">
div.row label{
    width: 60px;
    display: inline-block;
}
div.row span.msg{
    margin-left: 10px;
    color: #c2c2c2;
}
div.row input{
    height: 30px;
    font-size: 14px;
    margin: 5px;
}
div.row textarea{
     font-size: 14px;
     margin: 5px;
 }
</style>

<div class="detailContent" style="min-height: 1335px">
<?php
    if(empty($id)){
        $form=$this->beginWidget('CActiveForm', array(
            'id'=>'profiles-form',
            'htmlOptions'=>array('enctype'=>'multipart/form-data'),
            'enableAjaxValidation'=>false,
            'action'=>$this->createUrl('manage/adviser'),
        ));
    }else{
        $form=$this->beginWidget('CActiveForm', array(
            'id'=>'profiles-form',
            'htmlOptions'=>array('enctype'=>'multipart/form-data'),
            'enableAjaxValidation'=>false,
//            'action'=>$this->createUrl('manage/adviser'),
        ));
    }
?>

<?php echo $form->errorSummary($model); ?>


<div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username'); ?>
    <?php echo $form->error($model,'username'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'job'); ?>
    <?php echo $form->textField($model,'job'); ?>
    <?php echo $form->error($model,'job'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'nation'); ?>
    <?php echo $form->textField($model,'nation'); ?>
    <?php echo $form->error($model,'nation'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'birthday'); ?>
    <?php echo $form->textField($model,'birthday'); ?>
    <?php echo $form->error($model,'birthday'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'education'); ?>
    <?php echo $form->textField($model,'education'); ?>
    <?php echo $form->error($model,'education'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'info'); ?>
    <?php echo $form->textArea($model,'info',array('rows'=>6, 'cols'=>50)); ?>
    <?php echo $form->error($model,'info'); ?>
</div>

    <div class="row">
        <?php echo $form->labelEx($model,'photo'); ?>
        <?php echo $form->fileField($model,'photo',array('size'=>40,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'photo'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'order'); ?>
        <?php echo $form->textField($model,'order'); ?>
        <?php echo $form->error($model,'order'); ?>       <span class="msg">数字越小越靠前</span>
    </div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? '新增' : '保存'); ?>
</div>

<?php $this->endWidget(); ?>

    <div class="clear"></div>
</div>